###
#Load Data and Packages
###

library(lfe) 
library(stargazer)

load('congress.newspapers.rollcall.RData')

###
#Placebo Analysis for Congruence and DMA
###

rep.p <- felm(rep.approval.place ~ congruence + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(!is.na(rep.approval.place)))
sen.p <- felm(I((sen1.approval.place + sen2.approval.place)/2) ~ congruence + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(!is.na(rep.approval.place)))
gov.p <- felm(gov.approval.place ~ congruence + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(!is.na(rep.approval.place)))
pres.p <- felm(pres.approval.place ~ congruence + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(!is.na(rep.approval.place)))
house.p <- felm(house.majority.correct ~ congruence + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(!is.na(rep.approval.place)))
senmaj.p <- felm(senate.majority.correct ~ congruence + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(!is.na(rep.approval.place)))

#Table B1 - Appendix
stargazer(rep.p,sen.p,gov.p,pres.p,house.p,senmaj.p,keep=c('congruence'),column.labels=c('Rep','Sen','Gov','Pres','House Maj','Sen Maj'),covariate.labels=c('Newspaper Market Congruence'),notes=c('District-Year Fixed Effects','Individual and County Controls','Robust Standard Errors, Clustered by District-Year'),star.cutoffs=c(0.05),notes.align='l')

sen.tv <- felm(I((sen1.approval.place + sen2.approval.place)/2) ~  state.dma.share + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.year | 0 | state.year,data=congress.newspapers.rollcall,subset=which(!is.na(sen1.approval.place) & !is.na(sen2.approval.place)))
gov.tv <- felm(gov.approval.place ~ state.dma.share + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.year | 0 | state.year,data=congress.newspapers.rollcall,subset=which(!is.na(sen1.approval.place)  & !is.na(sen2.approval.place)))
rep.tv <- felm(rep.approval.place ~ state.dma.share + congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.year | 0 | state.year,data=congress.newspapers.rollcall,subset=which(!is.na(sen1.approval.place)  & !is.na(sen2.approval.place)))
pres.tv <- felm(pres.approval.place ~ state.dma.share + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.year | 0 | state.year,data=congress.newspapers.rollcall,subset=which(!is.na(sen1.approval.place)  & !is.na(sen2.approval.place)))
house.tv <- felm(house.majority.correct ~ state.dma.share + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.year | 0 | state.year,data=congress.newspapers.rollcall,subset=which(!is.na(sen1.approval.place)  & !is.na(sen2.approval.place)))
senmaj.tv <- felm(senate.majority.correct ~ state.dma.share + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.year | 0 | state.year,data=congress.newspapers.rollcall,subset=which(!is.na(sen1.approval.place)  & !is.na(sen2.approval.place)))

#Table C1 - Appendix
stargazer(sen.tv,gov.tv,rep.tv,pres.tv,house.tv,senmaj.tv,keep=c('state.dma.share'),column.labels=c('Sen','Gov','Rep','Pres','House Maj','Sen Maj'),covariate.labels=c('In-State DMA Share'),notes=c('State-Year Fixed Effects','Individual and County Controls','Robust Standard Errors, Clustered by State-Year'),star.cutoffs=c(0.05),notes.align='l')

###
#Newspapers and Issue Agreement with Representative
###

m1 <- felm(issue.summary ~ congruence + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall)

m2 <- felm(issue.summary ~ congruence + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(partisan.alignment > 4))

m3 <- felm(issue.summary ~ congruence + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(partisan.alignment==4))

m4 <- felm(issue.summary ~ congruence + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(partisan.alignment < 4))

#Table 5 - Main Text
stargazer(m1,m2,m3,m4,keep=c('congruence','issue.summary'),column.labels=c('All','Co-Partisans','Independents','Out-Partisans'),covariate.labels=c('Newspaper Market Congruence','Issue Agreement','Newspaper Market Congruence*Issue Agreement'),notes=c('District-Year Fixed Effects','Individual and County Controls','Robust Standard Errors, Clustered by District-Year'),star.cutoffs=c(0.05),notes.align='l',digits=2)

###
#Newspapers and Issue Agreement Interaction
###

m1 <- felm( rep.approval ~  congruence*issue.summary + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall)

m2 <- felm( rep.approval ~ congruence*issue.summary + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(partisan.alignment > 4))

m3 <- felm( rep.approval ~ congruence*issue.summary + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(partisan.alignment==4))

m4 <- felm( rep.approval ~ congruence*issue.summary + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(partisan.alignment < 4))

#Table 4 - Main Text
stargazer(m1,m2,m3,m4,keep=c('congruence','issue.summary'),column.labels=c('All','Co-Partisans','Independents','Out-Partisans'),covariate.labels=c('Newspaper Market Congruence','Issue Agreement','Newspaper Market Congruence*Issue Agreement'),notes=c('District-Year Fixed Effects','Individual and County Controls','Robust Standard Errors, Clustered by District-Year'),star.cutoffs=c(0.05),notes.align='l',digits=2)

##
#Focus on Voting Based Outcomes
##

pooled.model.rep.intent <- felm(rep.vote.intent ~ congruence*issue.summary + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall)

pooled.model.rep.vote <- felm(rep.vote.post ~ congruence*issue.summary + factor(partisan.alignment) + factor(ideological.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall)

#Table B4 - Appendix
stargazer(pooled.model.rep.intent,pooled.model.rep.vote,keep=c('congruence','issue.summary'),column.labels=c('All','Co-Partisans','Others'),covariate.labels=c('Newspaper Market Congruence','Issue Agreement','Newspaper Market Congruence*Issue Agreement'),notes=c('District-Year Fixed Effects','Individual and County Controls','Robust Standard Errors, Clustered by District-Year'),star.cutoffs=c(0.05),notes.align='l',digits=2)

##
#Temporal Variation
##

pooled.model.rep.approval.time1 <- felm(rep.approval ~ congruence*issue.summary + factor(partisan.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(year %in% c(2006,2008)))
pooled.model.rep.approval.time2 <- felm(rep.approval ~ congruence*issue.summary + factor(partisan.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(year %in% c(2010,2012)))
pooled.model.rep.approval.time3 <- felm(rep.approval ~ congruence*issue.summary + factor(partisan.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(year %in% c(2014,2016)))
pooled.model.rep.approval.time4 <- felm(rep.approval ~ congruence*issue.summary + factor(partisan.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall,subset=which(year %in% c(2018,2020)))

stargazer(pooled.model.rep.approval.time1,pooled.model.rep.approval.time2,pooled.model.rep.approval.time3,pooled.model.rep.approval.time4,keep=c('congruence','issue.summary'),column.labels=c('2006-2008','2010-2012','2014-2016','2018-2020'),covariate.labels=c('Newspaper Market Congruence','Issue Agreement','Newspaper Market Congruence*Issue Agreement'),notes=c('District-Year Fixed Effects','Individual and County Controls','Robust Standard Errors, Clustered by District-Year'),star.cutoffs=c(0.05),notes.align='l',digits=2)

interaction.coefs.name.house <- c('(2006-2008)','(2010-2012)','(2014-2016)','(2018-2020)')
interaction.coefs.point.house <- c(tail(pooled.model.rep.approval.time1$coefficients,1),tail(pooled.model.rep.approval.time2$coefficients,1),tail(pooled.model.rep.approval.time3$coefficients,1),tail(pooled.model.rep.approval.time4$coefficients,1))
interaction.coefs.se.house <- c(tail(pooled.model.rep.approval.time1$cse,1),tail(pooled.model.rep.approval.time2$cse,1),tail(pooled.model.rep.approval.time3$cse,1),tail(pooled.model.rep.approval.time4$cse,1))

house.temporal.estimates <- cbind.data.frame(interaction.coefs.name.house,interaction.coefs.point.house,interaction.coefs.se.house)
names(house.temporal.estimates) <- c('year','point','se')
house.temporal.estimates$lower <- house.temporal.estimates$point - 1.96*house.temporal.estimates$se
house.temporal.estimates$upper <- house.temporal.estimates$point + 1.96*house.temporal.estimates$se
save(house.temporal.estimates,file='house.temporal.estimates.RData')

load('senate.temporal.estimates.RData')

#Figure D1 - Appendix
pdf(file='temporal-estimates.pdf',height=4,width=6)
par(mar=c(2,4,1,1))
plot(x=c(1:4)-.1,y=house.temporal.estimates$point,pch=16,cex=1.5,xlim=c(.75,4.25),las=1,ylim=c(-.04,.20),xaxt='n',ylab='',xlab='')
points(x=c(1:4)+.1,y=senate.temporal.estimates$point,pch=16,cex=1.5,col='gray')
abline(h=0,lty=2)
segments(x0=c(1:4)-.1,y0=house.temporal.estimates$upper,y1=house.temporal.estimates$lower,lwd=4)
segments(x0=c(1:4)+.1,y0=senate.temporal.estimates$upper,y1=senate.temporal.estimates$lower,lwd=4,col='gray')
axis(side=1,at=1:4,labels=c('2006-2008','2010-2012','2014-2016','2018-2020'),cex.axis=1)
legend("topright",legend=c('Newspapers/House','TV/Senate'),col=c('black','gray'),pch=c(16,16),pt.cex=c(1.5,1.5),cex=.85)
dev.off()

##
#Discrete Variable Analysis
##

congress.newspapers.rollcall$congruence.bin <- NA
congress.newspapers.rollcall$congruence.bin[which(congress.newspapers.rollcall$congruence < .15)] <- 1
congress.newspapers.rollcall$congruence.bin[which(congress.newspapers.rollcall$congruence <= .64 & congress.newspapers.rollcall$congruence >= .15)] <- 2
congress.newspapers.rollcall$congruence.bin[which(congress.newspapers.rollcall$congruence > .64)] <- 3

quantile(congress.newspapers.rollcall$issue.summary,na.rm=TRUE)
congress.newspapers.rollcall$agreement.bin <- NA
congress.newspapers.rollcall$agreement.bin[which(congress.newspapers.rollcall$issue.summary <= .375)] <- 0
congress.newspapers.rollcall$agreement.bin[which(congress.newspapers.rollcall$issue.summary < .8 & congress.newspapers.rollcall$issue.summary > .375)] <- 1
congress.newspapers.rollcall$agreement.bin[which(congress.newspapers.rollcall$issue.summary >= .8)] <- 2

pooled.model.rep.approval.coarsened <- felm(rep.approval ~ factor(congruence.bin)*factor(agreement.bin) + factor(partisan.alignment) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=congress.newspapers.rollcall)

#Table B3 - Appendix
stargazer(pooled.model.rep.approval.coarsened,keep=c('congruence.bin','agreement.bin'),column.labels=c('All'),notes=c('District-Year Fixed Effects','Individual and County Controls','Robust Standard Errors, Clustered by District-Year'),star.cutoffs=c(0.05),notes.align='l',digits=2)
